/*
 * MP3_Beaney - The MP3 Tag Editor is a free ID3-Tag Editor
 * Copyright (C) 2012 Bernhard Teismann, André Seipelt, Steffen Gruschka, Imanuel Rohlfing
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. 
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package model;

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import org.jaudiotagger.tag.images.Artwork;
import org.jaudiotagger.tag.images.ArtworkFactory;

/**
 * Manages the ID3 Tags of MP3 Files.
 * <p/>
 * @author Steffen Gruschka
 * @author Bernhard Teismann
 * @author André Seipelt
 * @author Imanuel Rohlfing
 * @version 1.0
 */
public class MP3 {

    private String artist;      // Künstler
    private String title;       // Song-Titel
    private String album;       // Album-Titel
    private String release;     // Release
    private String genre;       // Genre
    private String comment;     // Kommentar
    private String track;       // Track Nummer
    private File path;          // Pfad zur MP3
    private File artwork;       // Pfad zum Cover
    final int COVER_SIZE = 135;
    final String COVER_ART = "Das Cover konnte nicht ausgelesen werden.";

    /**
     * Constructor. Creates an empty MP3 object.
     */
    public MP3(){
        artist = "";
        title = "";
        album = "";
        release = "";
        genre = "";
        comment = "";
        track = "0";
        path = null;
        artwork = null;
    }

    /**
     * Getter for album.
     * <p/>
     * @return album's name as string
     */
    public String getAlbum(){
        return album;
    }

    /**
     * Setter for album.
     * <p/>
     * @param album album's name as string
     */
    public void setAlbum(String album){
        this.album = album;
    }

    /**
     * Getter for album.
     * <p/>
     * @return artist's name as string
     */
    public String getArtist(){
        return artist;
    }

    /**
     * Setter for album.
     * <p/>
     * @param artist artist's name as string
     */
    public void setArtist(String artist){
        this.artist = artist;
    }

    /**
     * Getter for comment.
     * <p/>
     * @return comment text as string
     */
    public String getComment(){
        return comment;
    }

    /**
     * Setter for comment.
     * <p/>
     * @param comment comment text as string
     */
    public void setComment(String comment){
        this.comment = comment;
    }

    /**
     * Getter for genre.
     * <p/>
     * @return genre's name as string
     */
    public String getGenre(){
        return genre;
    }

    /**
     * Setter for genre.
     * <p/>
     * @param genre genre's name as string
     */
    public void setGenre(String genre){
        this.genre = genre;
    }

    /**
     * Getter for path.
     * <p/>
     * @return path as string
     */
    public File getPath(){
        return path;
    }

    /**
     * Getter for filename.
     * <p/>
     * @return name of path
     */
    public String getFileName(){
        return path.getName();
    }

    /**
     * Setter for path.
     * <p/>
     * @param path path as string
     */
    public void setPath(String path){
        this.path = new File(path);
    }

    /**
     * Getter for release.
     * <p/>
     * @return release's name as string
     */
    public String getRelease(){
        return release;
    }

    /**
     * Setter for release.
     * <p/>
     * @param release release's name as string
     */
    public void setRelease(String release){
        this.release = release;
    }

    /**
     * Getter for title.
     * <p/>
     * @return title as string
     */
    public String getTitle(){
        return title;
    }

    /**
     * Setter for title.
     * <p/>
     * @param title title's name as string
     */
    public void setTitle(String title){
        this.title = title;
    }

    /**
     * Getter for track.
     * <p/>
     * @return track's name as string
     */
    public String getTrack(){
        return track;
    }

    /**
     * Setter for track.
     * <p/>
     * @param track track's name as string
     */
    public void setTrack(String track){
        this.track = track;
    }

    /**
     * Creates an Artwork Object from the cover's path, which can be saved in the ID3 Tag.
     * Calls the method getImage() to get a BufferedImage.
     * <p/>
     * @return cover as BufferedImage if available, null if not
     */
    public BufferedImage getCoverAsImage(){
        if (artwork == null){
            return null;
        }else{
            try{
                return (BufferedImage) ArtworkFactory.createArtworkFromFile(artwork).getImage();
            }catch (Exception ex){
                return null;
            }
        }
    }

    /**
     * Getter for cover.
     * <p/>
     * @return artwork's name as string
     */
    public File getCover(){
        return artwork;
    }

    /**
     * Creates a new BufferedImage from the passed file and reduces its size to COVER_SIZE.
     * Said BufferedImage is then saved in the MP3's folder and is passed to the MP3 Object as its cover.
     * <p/>
     * @param cover file in which the cover image is contained
     * @throws MP3Exception
     * @see MP3Exception
     */
    public void setCover(File cover) throws MP3Exception{

        BufferedImage image = null;
        try{
            image = (BufferedImage) ImageIO.read(cover);

            float width = image.getWidth();
            float height = image.getHeight();
            int newWidth = (int) width;
            int newHeight = (int) height;

            if (width > COVER_SIZE || height > COVER_SIZE){
                if (width >= height){
                    newWidth = COVER_SIZE;
                    newHeight = (int) ((COVER_SIZE / width) * height);
                }else{
                    newWidth = (int) ((COVER_SIZE / height * width));
                    newHeight = COVER_SIZE;
                }
            }

            BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            newImage.getGraphics().drawImage(image, 0, 0, newWidth, newHeight, null);

            String dir = path.getAbsolutePath().substring(0, path.getAbsolutePath().lastIndexOf("."));
            ImageIO.write(newImage, "png", new File(dir + "-cover.png"));

            this.artwork = new File(dir + "-cover.png");
        }catch (Exception e){
            MP3Exception exception = new MP3Exception(COVER_ART, path);
            throw exception;
        }
    }

    /**
     * Creates a BufferedImage from the passed Artwork Object and reduces its size to COVER_SIZE.
     * Said BufferedImage is then saved in the MP3's folder and is passed to the MP3 Object as its cover.
     * <p/>
     * @param cover Artwork object in which the cover image is conatined.
     * @throws MP3Exception
     * @see MP3Exception
     */
    public void setCover(Artwork cover) throws MP3Exception{
        BufferedImage image = null;
        try{
            image = (BufferedImage) cover.getImage();

            float width = image.getWidth();
            float height = image.getHeight();
            int newWidth = (int) width;
            int newHeight = (int) height;

            if (width > COVER_SIZE || height > COVER_SIZE){
                if (width >= height){
                    newWidth = COVER_SIZE;
                    newHeight = (int) ((COVER_SIZE / width) * height);
                }else{
                    newWidth = (int) ((COVER_SIZE / height * width));
                    newHeight = COVER_SIZE;
                }
            }

            BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
            newImage.getGraphics().drawImage(image, 0, 0, newWidth, newHeight, null);

            String dir = path.getAbsolutePath().substring(0, path.getAbsolutePath().lastIndexOf("."));
            ImageIO.write(newImage, "png", new File(dir + "-cover.png"));
            artwork = new File(dir + "-cover.png");

        }catch (Exception e){
            MP3Exception exception = new MP3Exception(COVER_ART, path);
            throw exception;

        }
    }
}